options ls=80 nocenter;
filename indat 'asciiqob.txt';
libname save 'sasdata';

*goptions device=xcolor;


filename grafout pipe 'lpr -Pbrar';
goptions device=pslmono gsfname=grafout gsfmode=replace gaccess=sasgastd
         vsize=7 vorigin=3 ftext=centx;

/* read stripped ak-91 extract and process means for figures */

data zero;
 infile indat;
 input lwklywge educ yob qob pob;

proc means;
title '1980 qob extract';

data one;
 set zero;

%MACRO QTRSYR;
             QTR220 QTR320 QTR420        QTR221 QTR321 QTR421
             QTR222 QTR322 QTR422
             QTR223 QTR323 QTR423        QTR224 QTR324 QTR424
             QTR225 QTR325 QTR425
             QTR226 QTR326 QTR426        QTR227 QTR327 QTR427
             QTR228 QTR328 QTR428        QTR229 QTR329 QTR429
%mend;

LENGTH QTR120 3
       %QTRSYR 3
       YR20-YR29 3
       QTR1-QTR4 3;

ARRAY AQTR QTRSYR;
  DO OVER AQTR; AQTR = 0 ; END;

YR20 = ((YOB=30) OR (YOB=40));
YR21 = ((YOB=31) OR (YOB=41));
YR22 = ((YOB=32) OR (YOB=42));
YR23 = ((YOB=33) OR (YOB=43));
YR24 = ((YOB=34) OR (YOB=44));
YR25 = ((YOB=35) OR (YOB=45));
YR26 = ((YOB=36) OR (YOB=46));
YR27 = ((YOB=37) OR (YOB=47));
YR28 = ((YOB=38) OR (YOB=48));
YR29 = ((YOB=39) OR (YOB=49));

QTR1 = (QOB=1);
QTR2 = (QOB=2);
QTR3 = (QOB=3);
QTR4 = (QOB=4);

QTR120 = QTR1 * YR20; QTR220 = QTR2 * YR20; QTR320 = QTR3 * YR20;
QTR420 = QTR4 * YR20;
QTR121 = QTR1 * YR21; QTR221 = QTR2 * YR21; QTR321 = QTR3 * YR21;
QTR421 = QTR4 * YR21;
QTR122 = QTR1 * YR22; QTR222 = QTR2 * YR22; QTR322 = QTR3 * YR22;
QTR422 = QTR4 * YR22;
QTR123 = QTR1 * YR23; QTR223 = QTR2 * YR23; QTR323 = QTR3 * YR23;
QTR423 = QTR4 * YR23;
QTR124 = QTR1 * YR24; QTR224 = QTR2 * YR24; QTR324 = QTR3 * YR24;
QTR424 = QTR4 * YR24;
QTR125 = QTR1 * YR25; QTR225 = QTR2 * YR25; QTR325 = QTR3 * YR25;
QTR425 = QTR4 * YR25;
QTR126 = QTR1 * YR26; QTR226 = QTR2 * YR26; QTR326 = QTR3 * YR26;
QTR426 = QTR4 * YR26;
QTR127 = QTR1 * YR27; QTR227 = QTR2 * YR27; QTR327 = QTR3 * YR27;
QTR427 = QTR4 * YR27;
QTR128 = QTR1 * YR28; QTR228 = QTR2 * YR28; QTR328 = QTR3 * YR28;
QTR428 = QTR4 * YR28;
QTR129 = QTR1 * YR29; QTR229 = QTR2 * YR29; QTR329 = QTR3 * YR29;
QTR429 = QTR4 * YR29;

       **** CREATE STATE DUMMIES *** ;

LENGTH  AL AK AZ AR CA CO CT DE
        DC FL GA HI ID IL IN IA KA KY LA ME
        MD MA MI MN MS MO MT NE NV
        NH NJ NM NY NC ND OH OK OR PA
        RI SC SD TN TX UT VT VA WA WV WI WY  3;

%MACRO SOBDUM; AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KA KY LA ME
             MD MA    MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA
             RI SC SD TN TX UT VT VA WA WV WI WY
%mend;
%MACRO POBS; AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KA KY LA ME
             MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA
             RI SC SD TN TX UT VT VA WA WV WI WY
%mend;

ARRAY APOBS  AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KA KY LA ME
             MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA
             RI SC SD TN TX UT VT VA WA WV WI WY ;

DO OVER APOBS; APOBS=0; END;

IF POB<= 56 ;

IF POB = 1 THEN AL=1;
 ELSE IF POB =2  THEN AK = 1 ;
 ELSE IF POB =4  THEN AZ = 1 ;
 ELSE IF POB =5  THEN AR = 1 ;
 ELSE IF POB =6  THEN CA = 1 ;
 ELSE IF POB =8  THEN CO = 1 ;
 ELSE IF POB =9  THEN CT = 1 ;
 ELSE IF POB =10 THEN DE = 1 ;
 ELSE IF POB =11   THEN DC = 1 ;
 ELSE IF POB =12   THEN FL = 1 ;
 ELSE IF POB =13   THEN GA = 1 ;
 ELSE IF POB =15   THEN HI = 1 ;
 ELSE IF POB =16   THEN ID = 1 ;
 ELSE IF POB =17 THEN IL = 1 ;
 ELSE IF POB =18 THEN IN = 1 ;
 ELSE IF POB =19 THEN IA = 1 ;
 ELSE IF POB =20 THEN KA = 1 ;
 ELSE IF POB =21 THEN KY = 1 ;
 ELSE IF POB =22 THEN LA = 1 ;
 ELSE IF POB =23 THEN ME = 1 ;
 ELSE IF POB =24 THEN MD = 1 ;
 ELSE IF POB =25 THEN MA = 1 ;
 ELSE IF POB =26 THEN MI = 1 ;
 ELSE IF POB =27 THEN MN = 1 ;
 ELSE IF POB =28 THEN MS = 1 ;
 ELSE IF POB =29 THEN MO = 1 ;
 ELSE IF POB =30 THEN MT = 1 ;
 ELSE IF POB =31 THEN NE = 1 ;
 ELSE IF POB =32 THEN NV = 1 ;
 ELSE IF POB =33 THEN NH = 1 ;
 ELSE IF POB =34 THEN NJ = 1 ;
 ELSE IF POB =35 THEN NM = 1 ;
 ELSE IF POB =36 THEN NY = 1 ;
 ELSE IF POB =37 THEN NC = 1 ;
 ELSE IF POB =38 THEN ND = 1 ;
 ELSE IF POB =39 THEN OH = 1 ;
 ELSE IF POB =40 THEN OK = 1 ;
 ELSE IF POB =41 THEN OR = 1 ;
 ELSE IF POB =42 THEN PA = 1 ;
 ELSE IF POB =44 THEN RI = 1 ;
 ELSE IF POB =45 THEN SC = 1 ;
 ELSE IF POB =46 THEN SD = 1 ;
 ELSE IF POB =47 THEN TN = 1 ;
 ELSE IF POB =48 THEN TX = 1 ;
 ELSE IF POB =49 THEN UT = 1 ;
 ELSE IF POB =50 THEN VT = 1 ;
 ELSE IF POB =51 THE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3 ND_Q3 OH_Q3 OK_Q3 OR_Q3 PA_Q3
        RI_Q3 SC_Q3 SD_Q3 TN_Q3 TX_Q3 UT_Q3 VT_Q3 VA_Q3 WA_Q3 WV_Q3
        WI_Q3 WY_Q3
%mend;

ARRAY APOB_Q3  AL_Q3 AK_Q3 AZ_Q3 AR_Q3 CA_Q3 CO_Q3 CT_Q3 DE_Q3
        DC_Q3 FL_Q3 GA_Q3 HI_Q3 ID_Q3 IL_Q3 IN_Q3 IA_Q3 KA_Q3 KY_Q3
        LA_Q3 ME_Q3
        MD_Q3 MA_Q3 MI_Q3 MN_Q3 MS_Q3 MO_Q3 MT_Q3 NE_Q3 NV_Q3 NH_Q3
        NJ_Q3
        NM_Q3 NY_Q3 NC_Q3 ND_Q3 OH_Q3 OK_Q3 OR_Q3 PA_Q3
        RI_Q3 SC_Q3 SD_Q3 TN_Q3 TX_Q3 UT_Q3 VT_Q3 VA_Q3 WA_Q3 WV_Q3
        WI_Q3 WY_Q3  ;

DO OVER APOB_Q3; APOB_Q3 = APOBS*QTR3; END;
if ranuni(565869)<=.5 then educ2=educ;
keep yr21-yr29 %pob_q1 %pob_q2 %pob_q3 %qtrsyr educ lwklywge %sobdum educ2 qtr1-qtr3;

*******************************************************;



/* part I: 30 instrument case */

     proc syslin data=one 2sls;
     title '30-instrument case: regular 2sls';
     instruments yr21-yr29 %qtrsyr;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 educ/overid;

     proc syslin data=one liml;
     title 'LIML';
     instruments yr21-yr29 %qtrsyr;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 educ/overid;

     proc reg data=one ;
     title2 'first stage and OLS -- compute regular fits and leverage';
     first: model educ = yr21-yr29 %qtrsyr;
       output out=two h=lever p=edhat;
     ols: model lwklywge = yr21-yr29 educ;

     data two; set two
                      (keep=lwklywge educ yr21-yr29 edhat lever);
     keep lwklywge educ yr21-yr29 edhat1i edhat2i lever;
     edhat1i =  (edhat - (lever*educ));
     edhat2i =  edhat1i/(1-lever);

     proc syslin data=two 2sls;
     title 'second stage -- using feasible JIVE fitted values as instruments';
     instruments yr21-yr29 edhat1i;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 educ;

     proc syslin data=two 2sls;
     title 'second stage -- TRUE JIVE fitted values as instruments';
     instruments yr21-yr29 edhat2i;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 educ;

***> ussiv sequence;

     proc reg data=one ;
     title2 'first stage -- FOR USSIV';
     first: model educ2 = yr21-yr29 %qtrsyr;
       output out=two p=edhat2;

     proc syslin 2sls data=two;
     title 'USSIV results';
     where (educ2 eq .);
     instruments yr21-yr29 edhat2;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 educ;

/* part II: 180 instrument case */

     proc syslin data=one 2sls;
     title 'regular 2sls';
     instruments yr21-yr29 %sobdum %qtrsyr
                                   %pob_q1 %pob_q2 %pob_q3;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 %sobdum educ;

     proc syslin data=one liml;
     title 'LIML';
     instruments yr21-yr29 %sobdum %qtrsyr
                                   %pob_q1 %pob_q2 %pob_q3;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 %sobdum educ;

     proc reg data=one ;
     title1 '180-instrument case';
     title2 'first stage and OLS -- compute regular fits and leverage';
     first: model educ = yr21-yr29 %sobdum %qtrsyr
                                   %pob_q1 %pob_q2 %pob_q3;
       output out=two h=lever p=edhat;
     ols: model lwklywge = yr21-yr29 %sobdum educ;

     data two; set two
                    (keep=lwklywge educ yr21-yr29 %sobdum edhat lever);
     keep lwklywge educ yr21-yr29 %sobdum edhat1i edhat2i lever;
     edhat1i =  (edhat - (lever*educ));
     edhat2i =  edhat1i/(1-lever);

     proc syslin data=two 2sls;
     title 'second stage -- using feasible JIVE fitted values as instruments';
     instruments yr21-yr29 %sobdum edhat1i;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 %sobdum educ;

     proc syslin data=two 2sls;
     title 'second stage -- TRUE JIVE fitted values as instruments';
     instruments yr21-yr29 %sobdum edhat2i;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 %sobdum educ;

***> ussiv sequence;

     proc reg data=one ;
     title2 'first stage -- FOR USSIV';
     first: model educ2 = yr21-yr29 %sobdum %qtrsyr
                                   %pob_q1 %pob_q2 %pob_q3;
       output out=two p=edhat2;

     proc syslin 2sls data=two;
     title 'USSIV results';
     where (educ2 eq .);
     instruments yr21-yr29 %sobdum edhat2;
     endogenous lwklywge educ;
     model lwklywge = yr21-yr29 %sobdum educ;

run;
